<template>
		<view class="agree_jn">
			<!-- 步骤条 -->
			<uni-steps :options="steps" :active="activeNum" active-color="#00a660"></uni-steps>
			<!-- 审核中 -->
			<view v-if="status == 0 && certifyType != 2">
				<!-- 付款回单 -->
				<view class="authentication_header" style="color: #ff0000;">审核中 …… 工作日预计1小时左右通过审核！</view>
				<view class="authentication_payPic">
					<image :src="remitMoneyImg" @click="previewImg(remitMoneyImg)" class="imgPic"></image>
				</view>
				<!-- 按钮 -->
				<view class="btnArr">
					<button @click="back" size="mini" class="btn1">重新上传回单</button>
					<button @click="hurry" size="mini" class="btn1" style="margin-left: 20rpx;">催一下</button>
					<button @click="queryCompanyStatus" size="mini" class="btn2">查询认证结果</button>
				</view>
			</view>
			<!-- 企业实名认证失败 -->
			<view v-if="status == 2 && certifyType != 2">
				<view class="authentication_header" style="color: #ff0000;">企业实名认证失败！</view>
				<p class="reason">失败原因：</p>
				<p class="losup">{{ remark }}</p>
				<view>
					<button @click="back" class="payBtn" size="mini"> 已了解，去打款认证</button>
				</view>
			</view>
			<!-- 企业实名认证成功 -->
			<view v-if="status == 1">
				<view class="authentication_header text-primary">企业实名认证成功！</view>
				<p class="losup">恭喜您：<span>{{ remark == null ? '企业认证成功!' : remark }}</span></p>
				<view>
					<button @click="backIndex" class="payBtn">已了解，返回首页</button>
				</view>
			</view>
			<!-- 法定代表人授权认证中-->
			<view v-if="certifyType == 2 && status == 0">
				<view class="authentication_header" style="color: #ff0000;"> 法定代表人授权认证中</view>
				<view class="authentication_phone">
					<i class="iconfont themeColor" style="font-size: 100rpx">&#xe66a;</i>
					<p style="font-weight: 550;font-size: 30rpx;margin-bottom: 20rpx;">待法定代表人签署《法定代表人全业务授权书》</p>
					<p class="msg_gray"> 若法定代表人未收到平台发送的授权书通知短信，请检查法定代表人手机号码是否正确： </p>
					<p class="msg_gray" style="margin-top: 10rpx;"> 1、手机号正确，请点击【重新发送认证通知】按钮，再次发送授权通知。 </p>
					<p class="msg_gray"> 2、手机号错误，请点击【修改号码】按钮，再点击【重新发送认证通知】按钮，再次发送授权通知。 </p>
					<p class="msg_gray">3、您还可以将授权书链接通过微信、钉钉等方式发送给单位法定代表人签署，签署后，单位即可通过实名认证。</p>
					<view class="time-block">
						<view>短信发送时间：{{ ruleForm.createTime }}</view>
						<button @click="againSendMsg" class="back-Btn">重新发送</button>
					</view>
					<view class="gray-block">
						<view>法定代表人姓名：{{ ruleForm.legalPerson }}<br>实名手机号码：{{ruleForm.legalPersonMobile}}</view>
						<button @click="toOpenModifyMobileDialog" class="back-Btn">修改信息</button>
					</view>
					<!-- 按钮 -->
					<view class="footer-Btn">
						<button @click="revokeCert" class="cancel-Btn">撤销认证</button>
						<button @click="copyLegalSignUrl" class="next-Btn">复制认证链接</button>
						<button @click="queryCompanyStatus" class="next-Btn">查询认证结果</button>
					</view>
				</view>
			</view>

			<!-- 法定代表人授权认证中 修改法人手机号码弹出框 -->
			<uni-popup ref="modifyMobileDialog" type="dialog">
				<uni-popup-dialog ref="inputClose" mode="input" title="修改法定代表人手机号码" :value="mobile"
					placeholder="请输入修改法定代表人手机号码" @confirm="openUpLegalPersonMobileDialog"></uni-popup-dialog>
			</uni-popup>
		</view>
</template>

<script>
	import config from '@/config'
	import {
		againSendMsgLegalCert,
		companyHurry,
		copyLegalSignUrl,
		deleteRemitMoneyImg,
		getCertPageRecord,
		getInfoByCompanyId,
		revokeLegalCert,
		upLegalPersonMobile,
	} from "@/api/ec/work/company/companyCertification";
	import {
		goSignPageOrIndexMiXin
	} from "../../../plugins/goSignPageOrIndexMiXin";

	export default {
		mixins: [goSignPageOrIndexMiXin],
		components: {},
		data() {
			return {
        LegalSignUrl:null,
				// url地址
				host: config.baseUrl,
				// 认证步骤
				steps: [{
					title: '完善单位信息'
				}, {
					title: '核验单位信息'
				}, {
					title: '实名认证通过'
				}],
				//步骤条显示
				activeNum: 1,
				// 企业id
				companyId: '',
				// 1单位法定代表人 2单位授权代理人 3单位普通员工 
				managerType: 1,
				// 认证类型
				certifyType: 0,
				// 认证信息
				ruleForm: {
					legalPerson: "",
					legalPersonMobile: "",
					createTime: ""
				},
				// 法人修改过的手机号
				mobile: '',
				// 打款凭证图片
				remitMoneyImg: null,
				// 审核状态 0 审核中  1 审核通过  2 审核拒绝
				status: 0,
				// 备注
				remark: "",
			};
		},
		onLoad(option) {
			this.companyId = option.id
			this.managerType = option.managerType;
			this.certifyType = option.certifyType;
      // 创建法人复制链接
      this.initCopyLegalSignUrl();
		},
		created() {
			this.initData();
		},
		beforeDestroy() {
			clearInterval(this.timer);
		},
		methods: {
      initCopyLegalSignUrl() {
        if (this.certifyType == 2 && this.status == 0) {
          copyLegalSignUrl(this.companyId).then((response) => {
            this.LegalSignUrl = response.data;
          });
        }
      },
      /** 复制法人签署短链接 */
      copyLegalSignUrl() {
        this.$copyText(this.LegalSignUrl.companyName + "通知您签署《电子签约平台全业务授权书》，点击签署https://t.lksign.cn/" + this.LegalSignUrl.code)
            .then(
                () => {
                  this.$modal.msgSuccess("授权链接已复制到剪切板，可粘贴。");
                },
                () => {
                  this.$modal.msgError("复制失败！");
                }
            );
      },
			/** 查询认证结果 */
			queryCompanyStatus() {
				getInfoByCompanyId(this.companyId).then((response) => {
					let data = response.data;
					this.status = data.status;
					this.remark = data.remark;
					// 认证审核成功
					if (data.status == 1) {
						this.backIndex();
						this.activeNum = 3;
					}
					// 认证审核拒绝
					if (data.status == 2) {
						// clearInterval(_this.timer);
					}
					// 认证中
					if (data.status == 0) {
						// clearInterval(_this.timer);
						this.$modal.msg("单位实名认证中，请耐心等待！");
					}
				});
			},
			/** 根据公司id查询缓存的认证数据 */
			initData() {
				getCertPageRecord(this.companyId).then((response) => {
					this.ruleForm = response.data;
					this.mobile = this.ruleForm.legalPersonMobile
					// 不为空进行查询
					if (!!this.ruleForm.remitMoneyImg) {
						// 根据 路径获取oss文件
						this.remitMoneyImg = this.$ossUrl + this.ruleForm.remitMoneyImg;
					}
				});
			},
			/** 预览图片*/
			previewImg(path) {
				uni.previewImage({
					urls: [path],
					current: 0
				});
			},
			// /** 计时器轮训检查审核结果*/
			// initCerTtimer() {
			//   // 定时器
			//   this.timer = setInterval(() => {
			//     this.timerNumber++;
			//     getInfoByCompanyId(this.companyId).then((response) => {
			//       let data = response.data;
			//       this.status = data.status;
			//       this.remark = data.remark;
			//       // 认证审核成功
			//       if (data.status == 1) {
			//         clearInterval(this.timer);
			//         this.backIndex();
			//         this.activeNum = 3;
			//       }
			//       // 认证审核拒绝
			//       if (data.status == 2) {
			//         // clearInterval(_this.timer);
			//       }
			//       // 定时器超时次数
			//       if (this.timerNumber == 666) {
			//         clearInterval(this.timer);
			//       }
			//     });
			//   }, 5000);
			// },
			/**重新上传回单 */
			back() {
				let self = this;
				uni.showModal({
					title: '系统提示',
					confirmColor: '#00a660',
					confirmText: '确定',
					content: '是否重新上传回单!',
					success: function(res) {
						if (res.confirm) {
							// 删除回单文件
							deleteRemitMoneyImg(self.companyId).then(response => {
								clearInterval(self.timer);
								uni.redirectTo({
									url: '/pages/authentication/companyRealname/examine?id=' +
										self.companyId + '&managerType=' + self.managerType
								});
							});
						}
					}
				})
			},
			/** 返回首页 */
			backIndex() {
				this.$store.dispatch('SwitchCompany', this.companyId).then((res) => {
					this.$modal.msgSuccess("认证成功");
					this.$store.dispatch('GetInfo').then(res => {
						this.goSignPageOrIndex(res.user.mobile, 0);
					});
				});
			},
			/** 催他一下 */
			hurry() {
				companyHurry(this.ruleForm).then(response => {
					this.$modal.msgSuccess("系统已通知审核员，为您加快审核速度，请耐心等待！");
				});
			},
			/** 撤销认证 */
			revokeCert() {
				revokeLegalCert(this.ruleForm.companyId).then((response) => {
					this.$modal.msgSuccess("撤销认证");
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/authentication/companyRealname/start?id=' + this.companyId
						});
					}, 1000);
				});
			},
			/** 重新给法人发送短信 */
			againSendMsg() {
				againSendMsgLegalCert(this.ruleForm.companyId).then((response) => {
					this.$modal.msgSuccess("重新发送短信");
				});
			},
			/** 打开修改手机号码弹出框 */
			toOpenModifyMobileDialog() {
				this.$refs.modifyMobileDialog.open('center')
			},
			/** 打开修改法人手机号弹窗 */
			openUpLegalPersonMobileDialog(mobile) {
				this.mobile = mobile;
				if (!/^1[3456789]\d{9}$/.test(this.mobile)) {
					this.$modal.msgError("请输入正确的手机号码");
					return;
				}
				console.log(this.mobile)
				upLegalPersonMobile(this.mobile, this.companyId).then((response) => {
					this.ruleForm.legalPersonMobile = this.mobile;
					this.$refs.modifyMobileDialog.close()
					this.$modal.msgSuccess("修改成功,请重新发送通知");
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
	.gray-block {
		background: #f5f3f3;
		padding: 20rpx 30rpx;
		line-height: 40rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		text-align: left;
	}

	.time-block {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 40rpx auto 10rpx;
	}

	page {
		background-color: $uni-bg-color;
	}

	.reason {
		margin: 0;
		margin: 40rpx 8rpx 14rpx 8rpx;
	}

	.losup {
		color: #999999;
		font-size: 26rpx;
		margin: 20rpx 0;
	}

	// 按钮 
	.payBtn {
		background-color: $uni-color-primary;
		color: #fff;
		font-size: 26rpx;
		margin-top: 30rpx;
		position: fixed;
		width: 90%;
	}

	.btnArr {
		margin-top: 40rpx;
		width: 100%;

		.btn1 {
			color: $uni-color-primary;
			width: 48%;
			font-size: 30rpx;
			background: #fff;
			border: 1rpx solid $uni-color-primary;
		}

		.btn2 {
			background: $uni-color-primary;
			color: #fff;
			width: 99%;
			font-size: 30rpx;
			margin-top: 40rpx;
		}
	}

	.authentication_header {
		margin-top: 20rpx;
		padding: 20rpx;
		background-color: #edf7f1;
		font-size: 26rpx;
	}

	.authentication_payPic {
		text-align: center;
		margin: 40rpx auto;
		// .imgPic {
		// 	width: 600rpx;
		// 	height: auto;
		// 	display: block;
		// }
		
	}

	.agree_jn {
		padding: 40rpx 20rpx 0 20rpx;
		position: relative;
	}

	.authentication_phone {
		border-top: 1px solid #e1e1e1;
		text-align: center;
		padding: 30rpx 30rpx 20rpx 30rpx;

		.msg_gray {
			color: #999999;
			font-size: 26rpx;
			margin: 0 auto;
			text-align: left;
			text-indent: 1em;
			line-height: 46rpx;
		}
	}

	// 认证btn
	.footer-Btn {
		margin: 30rpx 0 0 0;
		display: flex;
		justify-content: space-between;
	}

	button {
		width: auto;
		font-size: 26rpx;
		line-height: 2.3;
	}

	.next-Btn {
		background-color: $uni-color-primary;
		color: #fff;
	}

	.back-Btn {
		border: solid 1rpx $uni-color-primary;
		color: $uni-color-primary;
		background: #fff;
	}
	.cancel-Btn{
		border: solid 1rpx rgb(247, 137, 137);
		color: #fff;
		background: rgb(247, 137, 137);
	}
</style>